const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [], //轮播图
    newsList: [], //资讯列表
    page: 1,
    infor: {}, //置顶资讯信息
    status: '' //货主/车主状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDate(); //轮播图
    this.getTopnew(); //置顶新闻
    this.getNews(); //新闻列表
  },
  //上拉刷新
  onReachBottom() {
    wx.showLoading({
      title: '加载中...',
      mark: true
    })
    setTimeout(() => {
      this.getNews();
    }, 500);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //我是货主
  goTruck() {
    console.log(this.data.status)
    if(wx.getStorageSync('openid') == ''||this.data.status == 400||this.data.status == 500) {
      wx.navigateTo({
        url: '/pages/trucks/warranty/warranty?type=201',
      })
    }
    if(this.data.status == 201 && wx.getStorageSync('openid') != '') {
      wx.reLaunch({
        url: '/pages/trucks/truckList/truckList'
      })
    }else if(this.data.status == 200 && wx.getStorageSync('openid') != ''){
      wx.showToast({
        title: '您已注册车主！',
        icon: 'none',
        mask: true
      })
    }
    
  },
  // 我是车主
  goCar(){
    console.log(this.data.status);
    if(wx.getStorageSync('openid') == ''||this.data.status == 400||this.data.status == 500) {
      wx.navigateTo({
        url: '/pages/trucks/warranty/warranty?type=200'
      })
    }
    if(this.data.status == 200 && wx.getStorageSync('openid') != '') {
      wx.reLaunch({
        url: '/pages/car/index/index'
      })
    }else if(this.data.status == 201 && wx.getStorageSync('openid') != ''){
      wx.showToast({
        title: '您已注册货主！',
        icon: 'none',
        mask: true
      })
    }
  },
  //置顶咨询详情
  goDetail() {
    wx.navigateTo({
      url: '/pages/infor/infor?id=' + this.data.infor.id
    })
  },
  //资讯列表详情
  messageDetail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/infor/infor?id=' + id
    })
  },
  //点击banner
  logan() {
    // wx.showToast({
    //   title: '请选择车主/货主！',
    //   icon: 'none',
		// 	mask: true
    // })
  },
  //判断车主/货主
  getRole() {
    wx.request({
      url: app.globalData.url + 'api/v1/wechat/getuser',
      method: 'GET',
      data: {
        open_id: wx.getStorageSync('openid')
      },
      success: res => {
        console.log(res)
        this.setData({
          status: res.data.code
        })
        console.log(this.status,123)
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //轮播图
  getDate() {
    wx.request({
      url: app.globalData.url + 'api/v1/news/index',
      method: 'GET',
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            bannerList: res.data.data
          })
        }
      },
      fail: err => {
        console.log(err)
      },
      complete: e => {
        console.log(e)
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    })
  },
  //置顶新闻
  getTopnew() {
    wx.request({
      url: app.globalData.url + 'api/v1/news/topnews',
      method: 'GET',
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            infor: res.data.data
          })
        }
      },
      fail: err => {
        console.log(err)
      },
      complete: e => {
        console.log(e)
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    })
  },
  //新闻列表
  getNews() {
    wx.request({
      url: app.globalData.url + 'api/v1/news/journalism',
      method: 'GET',
      data: {
        page: this.data.page,
        limit: 10
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200){
          if (this.data.page > 1){
            if( res.data.data.data.info != ''){
              this.setData({
                newsList: this.data.newsList.concat(res.data.data.data.info),
                page: this.data.page + 1
              })
            }
          }else{
            this.setData({
              newsList: res.data.data.data.info,
              page: this.data.page + 1
            })
          }
        }else{
          setTimeout(() => {
            wx.showToast({
              title: '没有更多数据',
              icon: 'none',
              mask: true
            })
          }, 500);
        }  
      },
      fail: err => {
        console.log(err)
      },
      complete: e => {
        console.log(e)
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('openid') != ''||this.data.status == '') {
      this.getRole()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page: 1
    })
    this.getDate();//轮播图
    this.getTopnew(); //置顶新闻
    this.getNews(); //新闻列表
    this.getRole();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})